package com.juzhencms.apps.busi.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.juzhencms.apps.base.common.database.DBTable;
import com.juzhencms.apps.base.common.util.WebUtil;

import lombok.extern.slf4j.Slf4j;


@Controller
@RequestMapping("/article")
@Slf4j
public class ArticleController extends BaseController {
		
		
		
		@RequestMapping(value="/detailData/{menu}",produces="text/html; charset=UTF-8")
		@ResponseBody
		public String detailData(@PathVariable("menu") String menu,HttpServletRequest request){
			Map record=this.getCommonService().getJdbcDao().queryRow("select * from "+DBTable.article+" where menu_id=? and del_flag=0 order by id asc limit 0,1", new Object[] {menu});
			
			//log.info(JSON.toJSONString(record));
			
			Map data=new HashMap();
			
			
			data.put("data", record);
			
			if(record!=null) {
				//获取最新版本数据
				Map lastHistory=this.getDataHistoryService().getLastValidVersion(DBTable.article, Integer.parseInt(record.get("id").toString()));
				//log.info(JSON.toJSONString(lastHistory));
				
				if(lastHistory!=null) {
					if(lastHistory.get("params")!=null) {
						//使用最新的版本字段替换掉当前主表字段进行显示
						Map param=JSON.parseObject(lastHistory.get("params").toString(),Map.class);
						if(param!=null) {
							for(Object key : param.keySet()) {
								if(param.get(key)!=null) {
									record.put(key, param.get(key));
								}
							}
						}
						lastHistory.remove("params");
					}
					data.put("lastHistory", lastHistory);
				}
			}
			
			
			
			
			
			
			return WebUtil.resultString(data,request);
			
			
		}
		@RequestMapping(value="/save",produces="text/html; charset=UTF-8")
		@ResponseBody
		public String save(HttpServletRequest request){
			boolean draft=false;
			
			if(request.getParameter("draft")!=null && request.getParameter("draft").equals("1")) {
				draft=true;
			}
			
			
			//System.out.println(request.getParameter("data"));
			Map data = JSON.parseObject(request.getParameter("data"), Map.class); 
			data.put("del_flag", 0);
			String id=null;
			if(data.get("id")!=null){
				id=data.get("id").toString();
			}
			
			//敏感词检查
			String sensitiveKeywords=this.filterSensitiveKeywords(DBTable.article, data);
			if(sensitiveKeywords!=null) {
				Map result=new HashMap();
				result.put("result", 0);
				result.put("id", id);
				result.put("msg", "内容中包含敏感关键词:<font style='color:#F00'>"+sensitiveKeywords+"</font>,请修改后再提交");
				return WebUtil.resultString(result,request);
			}
			
			if(data.get("site_id")==null || data.get("site_id").toString().equals("") || data.get("site_id").toString().equals("0")) {
				if(data.get("menu_id")!=null) {
					String menuId=data.get("menu_id").toString();
					Map menu=this.getCommonService().getJdbcDao().queryRow("select site_id from "+DBTable.menu+" where id=?", new Object[] {menuId});
					if(menu!=null) {
						data.put("site_id", menu.get("site_id"));
					}
				}
				
			}
			
			if(id==null || id.equals("")){
				
				if(data.get("create_time")==null || data.get("create_time").equals("")){
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					Date date = new Date();
					data.put("create_time", sdf.format(date));
				}
				//int articleid=this.getCommonService().getJdbcDao().addObj(DBTable.article, data);
				int articleid=0;
				if(draft) {
					articleid=this.getDataHistoryService().saveDraftData(DBTable.article, data,"article");
				}else {
					articleid=this.getDataHistoryService().saveCheckData(DBTable.article, data,"article");
				}
				
				
				data.put("id", articleid);
			}else{
				
				if(draft) {
					this.getDataHistoryService().saveDraftData(DBTable.article, data,"article");
				}else {
					this.getDataHistoryService().saveCheckData(DBTable.article, data,"article");
				}
			}
			
			
			
			
			Map result=new HashMap();
			result.put("result", 1);
			result.put("msg", "保存成功");
			
			return WebUtil.resultString(result,request);
		}
		
}
